package io.gitee.caoxiaoyu97.log.dynamic.level.log4j;

import io.gitee.caoxiaoyu97.log.dynamic.level.LogDynamicLevelEnhancer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;

/*********************************************************
 * 文件名称：BftsLevelMatchUtil.java
 * 系统名称：bfts
 * 模块名称：com.hundsun.tbsp.bfts.other.base.log.log4j
 * 功能说明：
 * 开发人员 @author：caoxy31978
 * 开发时间 @date：2025/9/23 14:01
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class Log4jLogDynamicLevelEnhancer extends LogDynamicLevelEnhancer {
    private static volatile Log4jLogDynamicLevelMatchFilter log4jLogDynamicLevelMatchFilter = null;
    private LoggerContext getLoggerContext() {
        return (LoggerContext) LogManager.getContext(false);
    }

    @Override
    protected void enhance() {
        if (log4jLogDynamicLevelMatchFilter == null){
            synchronized (Log4jLogDynamicLevelEnhancer.class){
                if (log4jLogDynamicLevelMatchFilter == null){
                    log4jLogDynamicLevelMatchFilter = Log4jLogDynamicLevelMatchFilter.newBuilder().build();
                    Configuration configuration = getLoggerContext().getConfiguration();
                    configuration.addFilter(log4jLogDynamicLevelMatchFilter);
                }
            }
        }

    }
}
